<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class user_manage_class extends AWS_MODEL
{

	function get_user_list($count = false, $where = '', $limit = '0,10', $orderby = 'uid DESC')
	{
		if ($count)
		{
			return $this->count('users', $where);
		}
		
		return $this->fetch_all('users', $where, $orderby, $limit);
	}

	
	function format_user_field($user_array)
	{
		if (is_array($user_array))
		{
			foreach($user_array as $key => $val)
			{
				if(empty($val))
				{
					continue;
				}
				
				if(in_array($key, array('reg_time', 'last_login', 'birthday')))
				{
					$user_array[$key] = strtotime($val);
				}
				
				if(in_array($key, array('reg_ip', 'last_ip')))
				{
					$user_array[$key] = ip2long($val);
				}
			}
		}
		
		return $user_array;
	}
	
	function get_group($where = '')
	{
		$rs =  $this->fetch_all('users_group', $where, 'group_id ASC');
		
		$group_list = array();
		
		foreach($rs as $key => $val)
		{
			$group_list[$val['group_id']] = $val;
		}
		
		return $group_list;
	}
	
	function update_group($where, $data)
	{
		return $this->update('users_group', $data, $where);
	}
	
	function add_group($group_name, $reputation_lower, $reputation_higer, $reputation_factor)
	{
		$data = array(
			'type' => 1,
			'custom' => 1,
			'group_name' => $group_name,
			'reputation_lower' => $reputation_lower,
			'reputation_higer' => $reputation_higer,
			'reputation_factor' => $reputation_factor,
		);
		
		return $this->insert('users_group', $data);
	}
	
	function remove_group($group_id)
	{
		return $this->delete('users_group', 'group_id = ' . intval($group_id));
	}
	
}
